<template>
  <div class="nav" v-if="false">
    <el-menu class="el-menu-vertical-demo" :default-active="currentIndex">
      <el-scrollbar style="height:100%">
        <el-menu-item
          :index="item.id.toString()"
          @click="handleSelect(item,index)"
          v-for="(item,index) in menuList"
          :key="item.id"
          v-show="item.LoadParameter == 1"
        >
          <img :src="item.ModulePicture" />
          <el-badge :value="item.newInfoCount" class="item" :hidden="item.newInfoCount == 0">
            <span>{{ item.ModuleName }}</span>
          </el-badge>
        </el-menu-item>
      </el-scrollbar>
    </el-menu>
  </div>
</template>

<script>
import { getModuleconf } from '@/api/data'
import { loginMaterials } from '@/api/event'
import { mapState } from 'vuex'
import store from '@/store'
export default {
  name: 'Nav',
  props: {
    showMenu: Boolean,
  },
  data() {
    return {
      menuList: [],
      currentIndex: '1',
    }
  },
  computed: mapState({
    operatorInfo: (state) => state.cache.operatorInfo,
  }),
  methods: {
    //初始化
    init() {
      this.menuList = JSON.parse(sessionStorage.getItem('menuList'))
      if (this.menuList == null || this.menuList == '') {
        return
      }
      this.currentIndex = this.menuList[0].id.toString()
      this.getCurrentStatus()
    },
    //路由跳转
    handleSelect(item, index) {
      if (item.newInfoCount > 0) {
        this.menuList[index].newInfoCount = 0
        sessionStorage.setItem('menuList', JSON.stringify(this.menuList))
      }
      if (item.ModuleType == 1) {
        this.$router.push(item.LoadPath)
      } else if (item.ModuleType == 2) {
        if (item.ModuleName == '应急物资') {
          this.handleGotoSupplies(item.LoadPath)
        } else if (item.ModuleName == '开发本地物资') {
          this.handleGotoSupplies(item.LoadPath)
        } else {
          window.open(item.LoadPath, '_blank') // 新窗口打开外链接
        }
      }
      this.$emit('handleShowMenuRoute')
    },
    //应急物资
    handleGotoSupplies(LoadPath) {
      const token = sessionStorage.getItem('tokenSign')
      const operator = this.operatorInfo
      const obj = {
        token: token,
        userName: operator.userName,
      }
      window.open(LoadPath + '?mess=' + token, '_blank') // 新窗口打开外链接
      // loginMaterials(obj).then((res) => {
      //   if (res.message == 'succ') {
      //     window.open(LoadPath + '?mess=' + token, '_blank') // 新窗口打开外链接
      //   } else {
      //     alert(callback.message)
      //   }
      // })
    },
    //刷新后重新定位选中状态
    getCurrentStatus() {
      let path = this.$route.path
      if (path) {
        let arr = this.menuList.filter((e) => {
          return e.LoadPath == path
        })
        if (arr.length > 0) {
          this.currentIndex = arr[0].id.toString()
        }
      }
    },
    //选中指定菜单
    handleCheckNav(path) {
      let _this = this
      this.menuList.forEach((e) => {
        if (e.LoadPath == path) {
          _this.currentIndex = e.id.toString()
        }
      })
      store.dispatch('navEvent') // vuex缓存
    },
  },
  mounted() {
    this.init()
  },
  created() {
    window['handleCheckNav'] = (result) => {
      this.handleCheckNav(result)
    }
  },
}
</script>

<style lang="scss">
@import '~@/assets/scss/nav.scss';
</style>
